package com.by.service.impl;

import com.by.bean.ProductEnquiry;
import com.by.common.bean.PageBean;
import com.by.common.crud.BaseServiceImpl;
import com.by.common.utils.Assert;
import com.by.common.utils.DateUtils;
import com.by.mapper.ProductEnquiryDetailMapper;
import com.by.mapper.ProductEnquiryMapper;
import com.by.mapper.SupplierEnquiryMapper;
import com.by.query.SupplierProductEnquiryQuery;
import com.by.service.ProductEnquiryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductEnquiryServiceImpl extends BaseServiceImpl<ProductEnquiry, ProductEnquiryMapper> implements ProductEnquiryService {
    final String PREFIX = "CPXJ";

    String maxCode = null;//当前最大编码

    @Autowired
    ProductEnquiryDetailMapper productEnquiryDetailMapper;
    @Autowired
    SupplierEnquiryMapper supplierEnquiryMapper;

    @Transactional
    @Override
    public int save(ProductEnquiry entity) {
        //生成编码
generateCode();
entity.setCode(maxCode);
        //保存询价单
        int rs = super.save(entity);
        Assert.isNotSuccess(rs, "产品询价单保存失败");
        //保存询价明细
        if (null != entity.getProductEnquiryDetailList() && entity.getProductEnquiryDetailList().size() > 0) {
            rs = productEnquiryDetailMapper.batchSave(entity.getId(), entity.getProductEnquiryDetailList());
            Assert.isNotSuccess(rs, "产品询价明细保存失败");
        }
        //生成供应商询价
        rs = supplierEnquiryMapper.save(entity.getId(), entity.getCategoryId());
        Assert.isNotSuccess(rs, "根据产品类别生成对应供应商的询价失败");

        return 1;
    }

    String generateCode() {
        //获取系统年月
        String ym = DateUtils.getYm();
//判断当前内存中是否存在最大编码
        if (null == maxCode) {
            //查询当月最大编码
            maxCode = baseMapper.queryMaxCode(ym);
            if (null == maxCode) {
                //说明是本月第一个订单
                maxCode = PREFIX + ym + "001";
                return maxCode;
            }
        }
        //不为空
        //获取当前最大编码的年月
        String tmpYm = maxCode.substring(4, 10);
        //比较最大编码的月份与当前系统时间
        if (!ym.equals(tmpYm)) {
//跨越了 归零
            maxCode = PREFIX + ym + "001";
            return maxCode;
        }
        //如果相等
        //获取递增序号
        String seq = maxCode.substring(10);
        Integer tmp = Integer.parseInt(seq);
        tmp++;
        maxCode = PREFIX + ym + String.format("%03d", tmp);
        return maxCode;
    }

    @Override
    public PageBean querySupplierProductEnquiry(SupplierProductEnquiryQuery query) {
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List list = baseMapper.querySupplierProductEnquiry(query);
        PageInfo info = new PageInfo(list);

        return new PageBean<>(query.getPage(), query.getPageSize(), info.getTotal(), list);
    }
}
